package com.itheima.Demo04;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class DemoBS001 {
    public static void main(String[] args) {
        try {
            ServerSocket ss = new ServerSocket(9876);
            while (true) {
                Socket s = ss.accept();
                //获取输入流对象;
                InputStream is = s.getInputStream();
                //获取一行字符串的方法;
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                String s1 = br.readLine();
                //切割获取来的字符串;
                System.out.println(s1);
                String[] s2 = s1.split(" ");
                String name = s2[1];
                System.out.println(name);
                //查找文件是否存在;
                File f = new File("Day18codes\\发车" + name);
                //创建输出流;
                BufferedOutputStream bos = new BufferedOutputStream(s.getOutputStream());
                if (f.exists()) {
                    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
                    bos.write("HTTP/1.1 200 OK\r\nContent-Type:image/jpeg\r\n\r\n".getBytes());
                    int i;
                    while ((i = bis.read()) != -1) {
                        bos.write(i);

                    }
                    bis.close();
                } else {
                    bos.write("HTTP/1.1 200 OK\r\nContent-Type:text/html;charset=UTF-8\r\n\r\n".getBytes());
                    bos.write("访问失败!".getBytes());

                }

                bos.close();
                s.close();


            }
        } catch (Exception e) {

        }

    }
}
